package Perlescent::Utils;

use Exporter qw(import);
use strict;
use warnings;
use DBI;
use DBD::mysql;
our(@ISA,@EXPORT,@EXPORT_OK);
	
@EXPORT = qw(
	     );
@EXPORT_OK = qw(
	        );

use lib qw( /var/pwneth/htdocs/perlescent );
my $conf = '/var/pwneth/config';
	
sub new
{
    my $class = shift;
    my $self = {};
    bless $self, $class;
    my $caller = caller;
    &writeLog("Utils","INFO","Creating new object for $caller");
    return $self;
}


sub writeLog
{
	  $|=1;
	  shift if ref($_[0]);
	  my($method,$lvl,$msg) = @_;
	  my $log = "/var/pwneth/htdocs/perlescent/logs/redmoon.log";
	  my $time = &timestamp;
		open(LOG, ">>$log")||return "$!";
			print LOG "[$time]\t$lvl\t$method\t$msg\n";
		close LOG;
}

sub dbConnect
{
	shift if ref($_[0]);
	my($db) = shift;
	my $user = "perlescent4dm";
	my $pw = "5t4R3E2w1q!";
	my $dsn = "dbi:mysql:$db:localhost:3306";
	my $dbh = DBI->connect($dsn, $user, $pw)
	or &writeLog("dbConnect","ERROR","Error connecting to $db");
	return $dbh;
}

sub getGeneric
{
	shift if ref($_[0]);
	my$data = shift;
	my$cond;
	my$table = $data->{table};
	my$field = $data->{field};

	while (my ($k,$v) = each %{$data})
	{
		next if ($k eq 'table' || $k eq 'field');
		next if ($k eq 'order_by' || $k eq 'limit');
		$cond .= "$k='$v' and ";
	}

	$cond =~ s/\sand\s$//g;

	my$order_by = "ORDER BY " .$data->{order_by} if ($data->{order_by});
	my$limit = "LIMIT " .$data->{limit} if ($data->{limit});

	my $dbh = &dbConnect("perlescent");
	my $sth = $dbh->prepare("select $field from $table where $cond")||return $dbh->errstr;
	$sth->execute()||return $sth->errstr;
	my $row = $sth->fetchrow;
	$sth->finish;
	$dbh->disconnect;
	return $row;
}

sub getGenericArray
{
	shift if ref($_[0]);
	my$data = shift;
	my$cond;
	my$table = $data->{table};
	my$field = $data->{field};

	while (my ($k,$v) = each %{$data})
	{
		next if ($k eq 'table' || $k eq 'field');
		next if ($k eq 'order_by' || $k eq 'limit');
		$cond .= "$k='$v' and ";
	}

	$cond =~ s/\sand\s$//g;

	my$order_by = "ORDER BY " .$data->{order_by} if ($data->{order_by});
	my$limit = "LIMIT " .$data->{limit} if ($data->{limit});

	my $dbh = &dbConnect("perlescent");
	my $sth = $dbh->prepare("select $field from $table where $cond $order_by $limit")||return $dbh->errstr;
	$sth->execute()||return $sth->errstr;
	my @rows = $sth->fetchrow_array;
	$sth->finish;
	$dbh->disconnect;
	return wantarray ? @rows : "@rows";
}

sub insGeneric
{
	shift if ref($_[0]);
	my$data = shift;
	my($fields,$values);
	my$table = $data->{table};

	while (my ($k,$v) = each %{$data})
	{
		next if ($k =~ /table/);
		$v = "'$v'" unless ($v =~ /[\(\)|\(.+\)]/);
		$fields .= "$k, ";
		$values .= "$v, ";
	}

	$fields =~ s/,\s$//g;
	$values =~ s/,\s$//g;

	my $dbh = &dbConnect("perlescent");
	my $sth = $dbh->prepare("insert into $table ( $fields ) values ( $values )");
	$sth->execute();
	$sth->finish;
	$dbh->disconnect;
	return 1;
}

sub upGeneric
{
	shift if ref($_[0]);
	my$data = shift;
	my($cond,$set);
	my$table = $data->{table};

	while (my ($k,$v) = each %{$data})
	{
		next unless ($k =~ /set/);
		#$v = "'$v'" unless ($v =~ /[\(\)|\(.+\)]/);
		$set .= "$v, ";
	}

	while (my ($k2,$v2) = each %{$data})
	{
		next if ($k2 eq 'table'||$k2 =~ /set/);
		#$v = "'$v'" unless ($v =~ /[\(\)|\(.+\)]/);
		$cond .= "$k2=$v2, and";
	}

	$cond =~ s/,\sand$//g;
	$set =~ s/,\s$//g;

	my $dbh = &dbConnect("perlescent");
	my $sth = $dbh->prepare("update $table set $set where $cond") || return $dbh->errstr;
	$sth->execute() || return $sth->errstr;
	$sth->finish;
	$dbh->disconnect;
	return 1;
}

sub timestamp
{
	my($sec,$min,$hr,$mday,$mon,$year) = localtime(time);
	$year = $year+1900;
	$mon = $mon+1;
		foreach my $x ($mon,$mday,$hr,$min,$sec)
		{
	    	$x = "0$x" if (length$x == 1);
		}
	my $ts = "$year-$mon-$mday $hr:$min:$sec";
	return $ts
}

return 1;
